package com.xusiyan08.repository;

import com.xusiyan08.entity.Item;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface ItemRepository extends JpaRepository<Item, Long> {
    List<Item> findBySellerId(Long sellerId);
    @Query("SELECT i FROM Item i WHERE i.status = :status")
    List<Item> findByStatus(@Param("status") Item.Status status);

    @Query("SELECT i FROM Item i WHERE i.title LIKE %:keyword% OR i.description LIKE %:keyword%")
    List<Item> searchByKeyword(@Param("keyword") String keyword);

    @Query("SELECT i FROM Item i WHERE i.status = :status ORDER BY i.createdAt DESC")
    List<Item> findAllAvailableItems(@Param("status") Item.Status status);

    // findWithImagesById is effectively replaced by findById, as images are now a direct attribute.
    // If specific image-related logic was intended beyond just loading the item, it needs to be re-evaluated.
    // For now, we assume findById is sufficient. If a distinct method is still needed, it would be:
    // @Query("SELECT i FROM Item i WHERE i.itemId = :itemId")
    // Optional<Item> findWithImagesById(@Param("itemId") Long itemId);
    // However, Spring Data JPA's findById should suffice.
    // Let's rename findWithImagesById to a more generic name or rely on default findById.
    // For minimal disruption, we can keep the method name but remove the fetch.
    @Query("SELECT i FROM Item i WHERE i.itemId = :itemId")
    Optional<Item> findWithImagesById(@Param("itemId") Long itemId);

    @Query("SELECT i FROM Item i ORDER BY i.createdAt DESC")
    List<Item> findAllWithImages();
}